/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.info.sinafinance;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author YangHua
 *
 */
public class InfoAdapter extends BaseAdapter {
	private List<InfoItem> infoItems;
	private LayoutInflater mInflater;
    private Bitmap icNew,icRead,icFav;
    private Context context;
    private Resources res;
    
	public InfoAdapter(Context context){
		res=context.getResources();
		mInflater = LayoutInflater.from(context);
		icNew = BitmapFactory.decodeResource(res, R.drawable.ic_new);
		icRead = BitmapFactory.decodeResource(res, R.drawable.ic_read);
		icFav = BitmapFactory.decodeResource(res, R.drawable.ic_fav);
		infoItems=new ArrayList<InfoItem>();
		this.context=context;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		// TODO Auto-generated method stub
		return infoItems.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return infoItems.get(position);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.icon = (ImageView) convertView.findViewById(R.id.item_icon);
			holder.summary = (TextView) convertView.findViewById(R.id.summary);
			holder.date = (TextView) convertView.findViewById(R.id.date);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		InfoItem info=infoItems.get(position);
		String summary=info.getSummary();
		holder.title.setText(info.getTitle());
		holder.summary.setText(Html.fromHtml(summary!=null&&!summary.trim().equals("")?summary:info.getTitle()));
		holder.date.setText(info.getPubDate());
		int status=info.getStatus();
		switch (status) {
			case InfoConstant.STATUS_NEW:
				holder.icon.setImageBitmap(icNew);
				break;
			case InfoConstant.STATUS_READ:
				holder.icon.setImageBitmap(icRead);
				break;
			case InfoConstant.STATUS_FAV:
				holder.icon.setImageBitmap(icFav);
				break;
		}
		return convertView;
    }
 	static class ViewHolder {
 		TextView title;
 		TextView summary;
 		TextView date;
 		ImageView icon;
 	}
 	
 	public void add(InfoItem infoItem){
 		if(infoItem!=null){
 			infoItems.add(infoItem);
 		}
 	}
}
